package org.lanqiao.dxzf.controller.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.lanqiao.dxzf.entity.Admin;
import org.lanqiao.dxzf.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {
	@Autowired
	AdminService adminService;
	
    @RequestMapping("/admin_login.do")
    public String adminLogin(Admin admin,HttpSession session){
    	System.out.println(admin.getAaccount()+" "+admin.getApsw());
        Admin reAdmin = adminService.login(admin);
       if(reAdmin!=null) {
            //登录成功,将个人信息存入session
            session.setAttribute("admin", reAdmin);
            System.out.println(reAdmin);
            return "index";
        }else {
            return "login";
        }
    }
    
    /**
     * 登录界面
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView helloWorld(){
        ModelAndView mav = new ModelAndView("login");
        return mav;
    }
    /**
     * 管理员管理-->查询管理员所有信息
     * @return
     */
    @RequestMapping("/listAdmin.do")
    public ModelAndView admin_list(){
        ModelAndView modelAndView = new ModelAndView("admin/admin_list");
        modelAndView.addObject("listAdmin",adminService.find(null));
        return modelAndView;
    }
    /**
     * 管理员——>>修改管理员信息页面
     * @return
     */
    @RequestMapping("/admin_modi.do")
    public String admin_modi(HttpServletRequest req,Model model) {
    	String aid = (String) req.getParameter("aid");
    	Admin admin = adminService.selectAdminByAid(Integer.parseInt(aid));
    	model.addAttribute("admin", admin);
    	return "admin/admin_modi";
    }
    @RequestMapping("/modiAdmin.do")//使用AJAX实现
    public String modiAdmin(Admin admin,Model model) {
    	adminService.update(admin);
    	model.addAttribute("listAdmin",adminService.find(null));
    	return "admin/admin_list"; //修改完返回管理员信息页面
    }
    /**
     * 修改密码界面
     * @return
     */
    @RequestMapping("/user_modi_pwd")
    public ModelAndView user_modi_pwd(){
        ModelAndView mav = new ModelAndView("user/user_modi_pwd");
        return mav;
    }
    /**
     * 管理员-->修改密码
     * @param admin
     * @param session
     * @return
     */
    @RequestMapping("/updatePsw.do")
    public String updatePsw(Admin admin,HttpSession session) {
    	adminService.update(admin);
    	session.removeAttribute("admin");//移除session
    	return "login";//修改完密码回到登录界面
    }
    /**
     * 个人信息界面
     * @return
     */
    @RequestMapping("/user_info")
    public String user_info(){
        return "user/user_info";
    }
    /**
     * 管理员-->修改个人信息
     * @param admin
     * @param session
     * @return
     */
    @RequestMapping("/updateInfo.do")
    public String updateInfo(Admin admin,HttpSession session) {
    	adminService.update(admin);
    	//未完成-->修改session内容
    	return "user/user_info";//修改完信息回到该界面
    }
    /**
     * 管理员管理
     * 个人信息添加界面
     * @return
     */
    @RequestMapping("/admin_add")
    public ModelAndView admin_add(){
        ModelAndView mav = new ModelAndView("admin/admin_add");
        return mav;
    }
	
}
